package com.ecube.mqtt.page.listener.swapListener;

import com.ecube.mqtt.mysql.Query;
import com.ecube.mqtt.service.SwapSendService;
import com.ecube.mqtt.utils.CommonConstant;
import com.ecube.mqtt.utils.PropertyUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class SwapStartResultListener implements ActionListener {
    private JComboBox env;
    private JTextField stationId;
    private JTextField vehicletext;
    private JTextField rfid;

    private JTextField vintext;

    private JComboBox result;
    private JComboBox reson;
    private JFrame start;
    private  String path;
    public SwapStartResultListener(JFrame start,
                                   JComboBox env,
                                   JTextField stationId,
                                   JTextField vehicletext,
                                   JComboBox result,
                                   JComboBox reson,
                                   String path) {
        this.start = start;
        this.env = env;
        this.stationId = stationId;
        this.vehicletext = vehicletext;
        this.result = result;
        this.reson = reson;
        this.path = path;
//        this.vehicletext = vehicletext;
//        this.vintext = vintext;
    }

    public void actionPerformed(ActionEvent e) {
        Map propertys = new HashMap();

        propertys.put("env",env.getSelectedItem().toString());
        propertys.put("stationId",stationId.getText());
        propertys.put("vehicle",vehicletext.getText());

        try {
            PropertyUtil.setProperty(propertys, CommonConstant.SWAP_CONFIG_PATH);
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
        Dimension dim2 = new Dimension(100, 30);
        Dimension dim3 = new Dimension(300, 30);

        //生成新界面
        JFrame startres = new JFrame();
        startres.setSize(400, 200);
        startres.setTitle("发送结果");//设置窗体标题
        startres.setDefaultCloseOperation(2);
        startres.setLocationRelativeTo(null);
        startres.setFont(new Font("宋体", Font.PLAIN, 14));  //宋体，正常风格，14号字体

        FlowLayout fl = new FlowLayout(FlowLayout.CENTER,30,30);
        //实例化流式布局类的对象
        startres.setLayout(fl);

        String results = "";
        if(result.getSelectedItem().toString().contains("1")){
            results = "1";
        }else if (result.getSelectedItem().toString().contains("2")){
            results = "2";
        }

        String resons = "";
        if(reson.getSelectedItem().toString().contains("1")){
            resons = "1";
        }else if (reson.getSelectedItem().toString().contains("2")){
            resons = "2";
        }else if (reson.getSelectedItem().toString().contains("3")){
            resons = "3";
        }else if (reson.getSelectedItem().toString().contains("4")){
            resons = "4";
        }else if (reson.getSelectedItem().toString().contains("5")){
            resons = "5";
        }else if (reson.getSelectedItem().toString().contains("6")){
            resons = "6";
        }else if (reson.getSelectedItem().toString().contains("7")){
            resons = "7";
        }else if (reson.getSelectedItem().toString().contains("8")){
            resons = "8";
        }else {
            resons = "null";
        }

        //创建组件
        JPanel jp1 = new JPanel();
        JPanel jp2 = new JPanel();
        SwapSendService cemqSendService = new SwapSendService();
        Boolean startresult = false;
        String orderNo = Query.getSwapOrderSN(env.getSelectedItem().toString(), stationId.getText());
        try {
            startresult = cemqSendService.startfail(env.getSelectedItem().toString(), stationId.getText(), orderNo, vehicletext.getText(), results, resons);
        } catch (InterruptedException ex) {
            System.out.println("启动发送失败");
            throw new RuntimeException(ex);
        }

        if (startresult) {
            JLabel message = new JLabel("启动发送成功");
            message.setFont(new Font("宋体", Font.PLAIN, 20));  //宋体，正常风格，14号字体
            message.setPreferredSize(dim3);
            jp1.add(message);
            startres.add(jp1, BorderLayout.CENTER);

            JButton close = new JButton("确定");
            close.setFont(new Font("宋体", Font.PLAIN, 18));
            //设置按键大小
            close.setSize(dim3);
            jp2.add(close);
            startres.add(jp2, BorderLayout.SOUTH);

            close.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    startres.dispose();
                }
            });

            startres.setResizable(false);
            startres.setVisible(true);

        } else {
            JLabel message = new JLabel("启动发送失败");
            message.setFont(new Font("宋体", Font.PLAIN, 20));  //宋体，正常风格，14号字体
            message.setPreferredSize(dim3);
            //将textName标签添加到窗体上
            jp1.add(message);
            startres.add(jp1, BorderLayout.CENTER);

            JButton close = new JButton("确定");
            close.setFont(new Font("宋体", Font.PLAIN, 18));
            //设置按键大小
            close.setSize(dim3);
            jp2.add(close);
            startres.add(jp2, BorderLayout.SOUTH);

            close.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    startres.dispose();
                }
            });

            startres.setResizable(false);
            startres.setVisible(true);

        }
    }
}